﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class management_Produt_Produt_Edit : System.Web.UI.Page
{
    static string mypath = "";
    static string bigpath = "";
    static string biggerpath = "";
    private int id = Convert.ToInt32(HttpContext.Current.Request.QueryString["p"]);
    private int cid = Convert.ToInt32(HttpContext.Current.Request.QueryString["cid"]);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();
            BindDrpClass();
        }
    }

    //绑定数据
    public void Bind()
    {
        Model.Product Info = BLL.Product.GetModel(id);
        mypath = myimg.ImageUrl = Info.ImageUrl;
        bigpath = myimg1.ImageUrl = Info.ImageUrl_big;
        biggerpath = myimg2.ImageUrl = Info.ImageUrl_bigger;
        txtTitle.Text = Info.Title;
        txtContent.Value = Info.Content;
        txtSortid.Text = Info.Sortid.ToString();
        ckbRecommend.Checked = Convert.ToBoolean(Info.IsRecommend);
        dropCategory.SelectedValue = Info.CId.ToString();
        txtPrice.Text = Info.Price.ToString();
        txtCanyuUser.Text = Info.CanyuUser.ToString();
        txtKucun.Text = Info.KuCun.ToString();
        txtMoney.Text = Info.Money.ToString();
    }

    //绑定顶级分类
    private void BindDrpClass()
    { 
        int total = 0;
        DataTable dt = BLL.Product_Category.Categorys_Get(0, int.MaxValue, 0, out total);
        dropCategory.Items.Clear();
        dropCategory.Items.Add(new ListItem("请选择分类", "0"));
        DataRow[] drs = dt.Select("FId= " + 0);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            //顶级分类显示形式
            classname = "╋" + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank = "├─";
            //递归子分类方法
            BindNode(sonparentid, dt, blank);
        }
        dropCategory.DataBind();
    }

    //绑定子分类
    private void BindNode(int parentid, DataTable dt, string blank)
    {
        DataRow[] drs = dt.Select("FId= " + parentid);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            classname = blank + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank2 = blank + "─";
            BindNode(sonparentid, dt, blank2);
        }
    }

    //提交按钮事件
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        Up_Image();
    }

    //处理图片的方法
    protected void Up_Image()
    {
        bool fileOk = false;
        string d = DateTime.Now.ToString("yyyyMMddHHmmss");
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
        string path = Server.MapPath("/userfiles/product/small/") + d + fileExtension;
        string big_path = Server.MapPath("/userfiles/product/big/") + "big" + d + fileExtension;
        string bigger_path = Server.MapPath("/userfiles/product/bigger/") + "bigger" + d + fileExtension;
        if (FileUpload1.HasFile)
        {
            string[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg", "jpeg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOk = true;
                }
            }
            if (fileOk)
            {
                if (myimg.ImageUrl != "/userfiles/temp/default.jpg")
                {
                    Obj.file_delete(myimg.ImageUrl);
                    Obj.file_delete(myimg1.ImageUrl);
                    Obj.file_delete(myimg2.ImageUrl);
                }
                PhotoControl.MakeThumbnail(FileUpload1.PostedFile, path, 160, 124);
                PhotoControl.MakeThumbnail(FileUpload1.PostedFile, big_path, 183, 134);
                PhotoControl.MakeThumbnail(FileUpload1.PostedFile, bigger_path, 300);
                mypath = "/userfiles/product/small/" + d + fileExtension;
                bigpath = "/userfiles/product/big/" + "big" + d + fileExtension;
                biggerpath = "/userfiles/product/bigger/" + "bigger" + d + fileExtension;
                updateinfo(mypath, bigpath, biggerpath);
            }
            else
            {
                lblmsg.CssClass = "errmsg";
                lblmsg.Text = "X 图片格式错误。";
            }
        }
        else
        {
            mypath = myimg.ImageUrl;
            bigpath = myimg1.ImageUrl;
            biggerpath = myimg2.ImageUrl;
            updateinfo(mypath, bigpath, biggerpath);
        }
    }

    //修改的方法
    protected void updateinfo(string mypath, string bigpath, string biggerpath)
    {
        Model.Product Info = new Model.Product();
        Info.Title = txtTitle.Text;
        Info.CId = Convert.ToInt32(dropCategory.SelectedValue);
        Info.Price =decimal.Parse(txtPrice.Text.ToString());
        Info.Content = txtContent.Value;
        Info.ImageUrl = mypath;
        Info.ImageUrl_big = bigpath;
        Info.ImageUrl_bigger = biggerpath;
        Info.UpTime = DateTime.Now;
        Info.Id = id;
        Info.IsRecommend = ckbRecommend.Checked;
        Info.CanyuUser = int.Parse(txtCanyuUser.Text.ToString());
        Info.KuCun = int.Parse(txtKucun.Text.ToString());
        Info.Sortid = Convert.ToInt32(txtSortid.Text.Trim());
        Info.Money = int.Parse(txtMoney.Text.ToString());
        BLL.Product.Update(Info);
        Response.Redirect("Produt_List.aspx?cid=" + dropCategory.SelectedValue);
    }

    //返回按钮事件
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Produt_List.aspx?cid=" + dropCategory.SelectedValue);
    }
}
